// views/mine/myorder/index.js
var BASE_URL = getApp().globalData.BASE_URL
var util = require('../../../utils/util');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    dingdanlist: "",
    current_page: '', //上拉加载更多当前页
    last_page: '', //上拉加载更多最后页
    showflag: false, //控制上拉加载更多的出现和隐藏,
    showfinish: false, //控制没有数据的出现和隐藏
    van_tab_id: '' //切换top标签列表的id
  },

  onChange: function (e) {
    console.log(e)
    var _this = this
    _this.setData({
      van_tab_id: e.detail.index
    })
    if (_this.data.current_page == _this.data.last_page) {
      _this.setData({
        showfinish: false,
      })
    }
    var userinfo = wx.getStorageSync("USER")
    wx.request({
      url: `${BASE_URL}/user/dingdan/id/${e.detail.index}.html`,
      method: "POST",
      data: {
        uid: userinfo.id,
        type: 'xiaochengxv',
      },
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        res.data.data.forEach(function (item) {
          item.time = util.formatTimeTwo(item.time, 'Y-M-D h:m:s');
        })
        console.log(res)

        _this.setData({ 
          dingdanlist: res.data.data,
          current_page: res.data.current_page,
          last_page: res.data.last_page,  
        })
      }
    })
  },


















  //去付款
  gopay: function (e) {
    var id = e.currentTarget.dataset.id
    this.data.dingdanlist.forEach(item => {
      if (item.id == id) {
        if (item.type == 'gongzheng') {
          wx.navigateTo({
            url: `/views/index/shop/certificate/paypage/third/index?id=${id}`
          })
        }
        if (item.type.substring(0, 2) == 'ZL') {
          wx.navigateTo({
            url: `/views/huodongdetail/paypage/index?id=${id}`
          })
        }
        if (item.type == 'dhl') {
          wx.navigateTo({
            url: `/views/index/shop/mailservice/paypage/index?id=${id}`
          })
        }
        if (item.type == 'dongjixin') {
          wx.navigateTo({
            url: `/views/index/shop/translationcopy/dongjidetail/paypage/index?id=${id}`
          })
        }
        if (item.type == 'visa_wenshu') {
          wx.navigateTo({
            url: `/views/index/shop/translationcopy/dongjidetail/paypage/index?id=${id}`
          })
        }
        if (item.type == 'jianli') {
          wx.navigateTo({
            url: `/views/index/shop/translationcopy/resumedetail/paypage/index?id=${id}`

          })
        }
        if (item.type == 'defuzuowen') {
          wx.navigateTo({
            url: `/views/index/shop/translationcopy/compositiondetail/paypage/index?id=${id}`
          })
        }
        if (item.type == 'zhengshu') {
          wx.navigateTo({
            url: `/views/index/shop/translationcopy/compositiondetail/paypage/index?id=${id}`
          })
        }
        if (item.type == 'fanyi_rengong') {
          wx.navigateTo({
            url: `/views/index/shop/translationcopy/compositiondetail/paypage/index?id=${id}`
          })
        }
        if (item.type == 'xiurun') {
          wx.navigateTo({
            url: `/views/index/shop/translationcopy/compositiondetail/paypage/index?id=${id}`
          })
        }
        if (item.type == 'visa') {
          wx.navigateTo({
            url: `/views/index/shop/visahandle/paypage/index?id=${id}`
          })
        }
        if (item.type == 'daihui') {
          wx.navigateTo({
            url: `/views/index/shop/visahandle/paypage/index?id=${id}`
          })
        }
        if (item.type == 'baoxian') {
          wx.navigateTo({
            url: `/views/index/shop/visahandle/paypage/index?id=${id}`
          })
        }
        if (item.type == 'aps_ziliao') {
          wx.navigateTo({
            url: `/views/paypage/index?id=${id}`
          })
        }
      }
    })
  },

  //订单详情
  order_detail: function (e) {
    var id = e.currentTarget.dataset.id
    this.data.dingdanlist.forEach(item => {
      if (item.id == id) {
        if (item.type == 'gongzheng') {
          wx.navigateTo({
            url: `/views/index/shop/certificate/jindupage/dingdandetail/index?id=${id}`
          })
        }
        if (item.type.substring(0, 2) == 'ZL') {
          wx.navigateTo({
            url: `./huodongdetail/index?id=${id}`
          })
        }
        if (item.type == 'dhl') {
          wx.navigateTo({
            url: `./mailservicedetail/index?id=${id}`
          })
        }
        if (item.type == 'dongjixin') {
          wx.navigateTo({
            url: `./translationcopydetail/index?id=${id}`
          })
        }
        if (item.type == 'visa_wenshu') {
          wx.navigateTo({
            url: `./translationcopydetail/index?id=${id}&type=wenshu`
          })
        }
        if (item.type == 'jianli') {
          wx.navigateTo({
            url: `./translationcopydetail/index?id=${id}&type=jianli`
          })
        }
        if (item.type == 'defuzuowen') {
          wx.navigateTo({
            url: `./defuzuowendetail/index?id=${id}`
          })
        }
        if (item.type == 'zhengshu') {
          wx.navigateTo({
            url: `./zhengshudetail/index?id=${id}&type=zhengshu`
          })
        }
        if (item.type == 'fanyi_rengong') {
          wx.navigateTo({
            url: `./huodongdetail/index?id=${id}`
          })
        }
        if (item.type == 'xiurun') {
          wx.navigateTo({
            url: `./zhengshudetail/index?id=${id}`
          })
        }
        if (item.type == 'visa') {
          wx.navigateTo({
            url: `./visahandledetail/index?id=${id}`
          })
        }
        if (item.type == 'daihui') {
          wx.navigateTo({
            url: `./euroexchangedetail/index?id=${id}`
          })
        }
        if (item.type == 'aps_ziliao') {
          item.info=JSON.parse( item.info)
          wx.navigateTo({
            url: `../../txtdetail/index?id=${item.info.pid}`
          })
        }
        if (item.type == 'baoxian') {
          if(item.title.indexOf('公立')!==-1){
            wx.navigateTo({
              url: `./insurancedetail/index?id=${id}&type=gongli`
            })
          }else{
            wx.navigateTo({
              url: `./insurancedetail/index?id=${id}&type=sili`
            })
          }
        
        }
      }
    })
  },
  //公证进度
  gojindu: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/views/index/shop/certificate/jindupage/index?id=${id}`
    })
  },




















  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    var userinfo = wx.getStorageSync("USER")
    wx.request({
      url: `${BASE_URL}/user/dingdan/id/0.html`,
      method: "POST",
      data: {
        uid: userinfo.id,
        type: 'xiaochengxv',
      },
      header: {
        'Content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        res.data.data.forEach(function (item) {
          item.time = util.formatTimeTwo(item.time, 'Y-M-D h:m:s');
        })
        console.log(res)
        _this.setData({
          dingdanlist: res.data.data,
          current_page: res.data.current_page,
          last_page: res.data.last_page,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: util.throttle(function () {
      var _this = this
      if (_this.data.current_page !== _this.data.last_page) {
        _this.setData({
          current_page: _this.data.current_page + 1,
          showflag: true,
          showfinish: false,
        })
      } else {
        _this.setData({
          showflag: false,
          showfinish: true,
        })
      }
      if (_this.data.showflag) {
        var userinfo = wx.getStorageSync("USER")
        setTimeout(function () {
          wx.request({
            url: `${BASE_URL}/user/dingdan/id/${_this.data.van_tab_id}.html?page=${_this.data.current_page}`,
            data: {
              uid: userinfo.id,
              type: 'xiaochengxv',
            },
            header: {
              'Content-Type': 'application/json', //GET方式是这个
            },
            method: "POST",
            success(res) {
              res.data.data.forEach(function (item) {
                item.time = util.formatTimeTwo(item.time, 'Y-M-D h:m:s');
              })
              _this.setData({
                dingdanlist: _this.data.dingdanlist.concat(res.data.data),
                showflag: false,
              })
            }
          })
        }, 500)
      }
    },
    2000),

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})